<template>
    <div>
        <el-table :data="store.getters['specss/getSpecss']" border style="width: 100%">
            <el-table-column prop="id" label="规格编号" width="180">
            </el-table-column>
            <el-table-column prop="specsname" label="规格名称" width="180">
            </el-table-column>
            <el-table-column label="规格属性" width="180">
                <template v-slot="item">
                    <div>
                        <p v-for="attr in item.row.attrs" :key="attr">{{ attr }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template v-slot="item">
                    <div>
                        <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination background layout="prev, pager, next" :total="store.getters['specss/getCount']"
            :page-size="store.getters['specss/getSize']" @current-change="changePage" />
    </div>
</template>
<script lang='ts' setup>
import { defineEmits } from 'vue'
import { specsDelete } from '../../request/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
let store = useStore()

const emit = defineEmits(['edit'])
const edit = (id: number) => {
    emit('edit', id)
}

//封装一个删除事件
const del = (id: number) => {
    specsDelete({ id }).then(res => {
        if (res.code == 200) {
            ElMessageBox.confirm(
                '你确定要删除这条数据吗?',
                '警告',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    ElMessage({
                        type: 'success',
                        message: '删除',
                    })
                    store.dispatch('specss/getSpecssListActions')
                    store.dispatch('specss/getSpecssCount')
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消删除',
                    })
                })
        }
    })
}

//切换页码获取当前页码事件
const changePage = (n: number) => {
    // console.log(n,'999');
    store.dispatch('specss/changePageAction', n)
}

const getSpecssList = () => {
    store.dispatch('specss/getSpecssListActions')
}
getSpecssList()

const getSpecssCount = () => {
    store.dispatch('specss/getSpecssCount')
}
getSpecssCount()

</script>
<style lang='less' scoped>
.el-pagination {
    float: right;
    margin: 10px 10px 0 0;
}
</style>